package com.vates.vsp.practico.alumnos.business;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.model.ListDataModel;

import com.vates.vsp.practico.alumnos.service.AlumnoService;
import com.vates.vsp.practico.alumnos.view.AlumnosView;
import com.vates.vsp.practico.commons.UseCaseMode;

@ManagedBean(name = "alumnosListener")
@SessionScoped
public class AlumnosListener {

	@ManagedProperty(value = "#{alumnosView}")
	private AlumnosView alumnosView;

	@ManagedProperty(value = "#{alumnoService}")
	private AlumnoService alumnoService;

	public AlumnoService getAlumnoService() {
		return alumnoService;
	}

	public void setAlumnoService(AlumnoService alumnoService) {
		this.alumnoService = alumnoService;
	}

	public AlumnosView getAlumnosView() {
		return alumnosView;
	}

	public void setAlumnosView(AlumnosView alumnosView) {
		this.alumnosView = alumnosView;
	}

	public String mostrarAlumnos() {
		alumnosView.setUseCaseMode(UseCaseMode.CONSULTA);
		alumnosView.setAlumnosListData(new ListDataModel<Alumno>(alumnoService
				.getAlumnos()));
		return "listaAlumnos";
	}

	public String mostrarAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.CONSULTA);
		alumnosView.setAlumno(alumnosView.getAlumnosListData().getRowData());
		return "alumno";
	}

	public String modificarAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.MODIFICACION);
		alumnosView.setAlumno(alumnosView.getAlumnosListData().getRowData());
		return "alumno";
	}

	public String eliminarAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.ELIMINAR);
		alumnosView.setAlumno(alumnosView.getAlumnosListData().getRowData());
		return "alumno";
	}

	public void eliminar() {
		try {
			alumnoService.eliminar(alumnosView.getAlumno());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.mostrarAlumnos();
	}

	public String agregarAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.NUEVO);
		alumnosView.setAlumno(new Alumno());
		return "alumno";
	}

	public void guardarAlumno() {
		try {
			alumnoService.guardar(alumnosView.getAlumno());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.mostrarAlumnos();
	}

	public String volver() {
		return "inicio";
	}

	public String volverLista() {
		return "listaAlumnos";
	}

	public String salir() {
		return "salida";
	}

}
